package com.yantong.quartz.job;

import com.yantong.constant.BalanceLogFlowType;
import com.yantong.constant.BalanceLogStyle;
import com.yantong.pojo.BalanceLog;
import com.yantong.service.BalanceLogService;
import com.yantong.pojo.Member;
import com.yantong.pojo.MemberWallet;
import com.yantong.pojo.MemberWalletLog;
import com.yantong.enums.WalletLogOperationTypeEnum;
import com.yantong.enums.WalletLogTypeEnum;
import com.yantong.service.MemberService;
import com.yantong.service.MemberWalletService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author liaozhaogui
 * @description: AssetTimer
 * @date 2020/9/23 9:57
 */
@Service
public class AssetTimer {
    private static final Logger logger = LoggerFactory.getLogger(AssetTimer.class);
    @Autowired
    private MemberWalletService memberWalletService;
    @Autowired
    private BalanceLogService balanceLogService;
    @Autowired
    private MemberService memberService;

    private boolean taskOn = true;

    /**
     * 每天凌晨2点资产随机释放业务处理
     */
    @Scheduled(cron = "0 0 2 * * ?")
    //@Scheduled(cron = "0 24 16 * * ?")
    public void assetReleaseFlow() {
        if (!taskOn) {
            logger.info("system task set off!calcAchieveFlow!");
            return;
        }

        //查询所有的用户钱包
        List<MemberWallet> list = memberWalletService.queryAll();
        Random ra = new Random();
        for (MemberWallet memberWallet : list) {
            //查询用户信息
            Member member = memberService.getDetail(memberWallet.getMemberId());
            if (member != null) {
                //资产信息全为0的用户不进入释放逻辑
                if (memberWallet.getAA().compareTo(BigDecimal.ZERO) > 0 || memberWallet.getACA().compareTo(BigDecimal.ZERO) > 0 ||
                        memberWallet.getAQ().compareTo(BigDecimal.ZERO) > 0 || memberWallet.getBanBPay().compareTo(BigDecimal.ZERO) > 0) {
                    //资产配置释放到活跃资产  随机释放的取值范围为1%-5%
                    double proportionACA = ra.nextInt(5) + 1;
                    BigDecimal ACA = BigDecimal.ZERO;
                    if (memberWallet.getAA().compareTo(BigDecimal.ZERO) > 0) {
                        ACA = memberWallet.getAA().multiply(BigDecimal.valueOf(proportionACA / 1000)).setScale(4, BigDecimal.ROUND_DOWN);
                    }

                    //活跃资产释放到资产量化  随机释放的取值范围为5%-10%
                    double proportionAQ = ra.nextInt(6) + 5;
                    BigDecimal AQ = BigDecimal.ZERO;
                    if (memberWallet.getACA().compareTo(BigDecimal.ZERO) > 0) {
                        AQ = memberWallet.getACA().multiply(BigDecimal.valueOf(proportionAQ / 1000)).setScale(4, BigDecimal.ROUND_DOWN);
                    }

                    //资产量化释放到电子钱包  按照1%的比例
                    double proportionBanBPay = 1;
                    BigDecimal BanBPay = BigDecimal.ZERO;
                    if (memberWallet.getAQ().compareTo(BigDecimal.ZERO) > 0) {
                        BanBPay = memberWallet.getAQ().multiply(BigDecimal.valueOf(proportionBanBPay / 1000)).setScale(4, BigDecimal.ROUND_DOWN);
                    }

                    //电子钱包释放到红利资产  按照2%的比例
                    double proportionAD = 1;
                    BigDecimal AD = BigDecimal.ZERO;
                    if (memberWallet.getBanBPay().compareTo(BigDecimal.ZERO) > 0) {
                        AD = memberWallet.getBanBPay().multiply(BigDecimal.valueOf(proportionAD / 1000)).setScale(4, BigDecimal.ROUND_DOWN);
                    }

                    memberWallet.setAA(memberWallet.getAA().subtract(ACA));
                    memberWallet.setACA(memberWallet.getACA().subtract(AQ).add(ACA));
                    memberWallet.setAQ(memberWallet.getAQ().subtract(BanBPay).add(AQ));
                    memberWallet.setBanBPay(memberWallet.getBanBPay().subtract(AD).add(BanBPay));
                    memberWallet.setAD(memberWallet.getAD().add(AD));
                    memberWalletService.update(memberWallet);

                    //添加资产释放历史记录
                    //资产配置释放记录
                    if (ACA.compareTo(BigDecimal.ZERO) > 0) {
                        memberWalletService.insertLog(new MemberWalletLog(memberWallet.getEADD(), memberWallet.getMemberId(), WalletLogTypeEnum.AA.getCode(),
                                WalletLogOperationTypeEnum.EXPENDITURE.getCode(), ACA, proportionACA));
                    }

                    //活跃资产释放记录
                    //收入
                    if (ACA.compareTo(BigDecimal.ZERO) > 0) {
                        memberWalletService.insertLog(new MemberWalletLog(memberWallet.getEADD(), memberWallet.getMemberId(), WalletLogTypeEnum.ACA.getCode(),
                                WalletLogOperationTypeEnum.INCOME.getCode(), ACA, proportionACA));
                    }
                    //支出
                    if (AQ.compareTo(BigDecimal.ZERO) > 0) {
                        memberWalletService.insertLog(new MemberWalletLog(memberWallet.getEADD(), memberWallet.getMemberId(), WalletLogTypeEnum.ACA.getCode(),
                                WalletLogOperationTypeEnum.EXPENDITURE.getCode(), AQ, proportionAQ));
                    }

                    //资产量化释放记录
                    //收入
                    if (AQ.compareTo(BigDecimal.ZERO) > 0) {
                        memberWalletService.insertLog(new MemberWalletLog(memberWallet.getEADD(), memberWallet.getMemberId(), WalletLogTypeEnum.AQ.getCode(),
                                WalletLogOperationTypeEnum.INCOME.getCode(), AQ, proportionAQ));
                    }
                    //支出
                    if (BanBPay.compareTo(BigDecimal.ZERO) > 0) {
                        memberWalletService.insertLog(new MemberWalletLog(memberWallet.getEADD(), memberWallet.getMemberId(), WalletLogTypeEnum.AQ.getCode(),
                                WalletLogOperationTypeEnum.EXPENDITURE.getCode(), BanBPay, proportionBanBPay));
                    }

                    //电子钱包释放记录
                    //收入
                    if (BanBPay.compareTo(BigDecimal.ZERO) > 0) {
                        memberWalletService.insertLog(new MemberWalletLog(memberWallet.getEADD(), memberWallet.getMemberId(), WalletLogTypeEnum.BanBPay.getCode(),
                                WalletLogOperationTypeEnum.INCOME.getCode(), BanBPay, proportionBanBPay));
                    }
                    //支出
                    if (AD.compareTo(BigDecimal.ZERO) > 0) {
                        memberWalletService.insertLog(new MemberWalletLog(memberWallet.getEADD(), memberWallet.getMemberId(), WalletLogTypeEnum.BanBPay.getCode(),
                                WalletLogOperationTypeEnum.EXPENDITURE.getCode(), AD, proportionAD));
                    }

                    //资产配置释放记录
                    //收入
                    if (AD.compareTo(BigDecimal.ZERO) > 0) {
                        memberWalletService.insertLog(new MemberWalletLog(memberWallet.getEADD(), memberWallet.getMemberId(), WalletLogTypeEnum.AD.getCode(),
                                WalletLogOperationTypeEnum.INCOME.getCode(), AD, proportionAD));

                        //红利资产进入余额
                        BigDecimal credit2Old = member.getCredit2();
                        member.setCredit2(member.getCredit2().add(AD));
                        memberService.updateByPrimaryKeySelective(member);

                        memberWalletService.insertLog(new MemberWalletLog(memberWallet.getEADD(), memberWallet.getMemberId(), WalletLogTypeEnum.AD.getCode(),
                                WalletLogOperationTypeEnum.EXPENDITURE.getCode(), AD, proportionAD));

                        //红利资产进入余额记录新增
                        BalanceLog supplyBalanceLog = new BalanceLog();
                        supplyBalanceLog.setMid(memberWallet.getMemberId());
                        supplyBalanceLog.setMobile(member.getMobile());
                        supplyBalanceLog.setRealname(member.getRealname());
                        supplyBalanceLog.setStatus(0);
                        supplyBalanceLog.setType(1);
                        supplyBalanceLog.setStyle(BalanceLogStyle.BanBPay.getCode());
                        supplyBalanceLog.setCredittype("credit2");
                        supplyBalanceLog.setBeforeamount(credit2Old);
                        supplyBalanceLog.setMoney(AD);
                        supplyBalanceLog.setAfteramount(credit2Old.add(AD));
                        supplyBalanceLog.setCreateTime(new Date());
                        supplyBalanceLog.setCentreId(member.getCentreId());
                        supplyBalanceLog.setTitle("电子钱包");
                        supplyBalanceLog.setRemark("电子钱包红利资产转入");
                        supplyBalanceLog.setSource(0);
                        supplyBalanceLog.setFlowtype(BalanceLogFlowType.BanBPay.getCode());
                        balanceLogService.save(supplyBalanceLog);
                    }
                }
            }
        }
    }

    public static boolean IS_QUOTA_RELEASE_RUN = false;

}
